﻿@{
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
}

@model StoreOutput

<form class="layui-form layui-form-pane">
    @Html.AntiForgeryToken()
    <input type="hidden" value="@Model.Id" name="Id" />
    <div class="layui-form-item">
        <label class="layui-form-label">学校名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="Name" value="@Model.Name" lay-verify="required" required>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">省份</label>
            <div class="layui-input-inline">
                <select id="ProvinceCode" name="ProvinceCode" lay-filter="Place" lay-search="">
                    <option value="">请选择</option>
                    @foreach (var item in ViewBag.Province)
                    {
                        <option value="@item.Code" @(item.Code == Model.ProvinceCode ? "selected=selected" : "")>@item.Name</option>
                    }
                </select>
                <input type="hidden" name="ProvinceName" value="@Model.ProvinceName" />
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-inline">
                <select id="CityCode" name="CityCode" lay-filter="Place" lay-search="">
                    <option value="">请选择</option>
                    @foreach (var item in ViewBag.City)
                    {
                        <option value="@item.Code" @(item.Code == Model.CityCode ? "selected=selected" : "")>@item.Name</option>
                    }
                </select>
                <input type="hidden" name="CityName" value="@Model.CityName" />
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">地区</label>
            <div class="layui-input-inline">
                <select id="DistrictCode" name="DistrictCode" lay-filter="Place" lay-search="">
                    <option value="">请选择</option>
                    @foreach (var item in ViewBag.District)
                    {
                        <option value="@item.Code" @(item.Code == Model.DistrictCode ? "selected=selected" : "")>@item.Name</option>
                    }
                </select>
                <input type="hidden" name="DistrictName" value="@Model.DistrictName" />
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="Address" value="@Model.Address">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">加盟时间</label>
        <div class="layui-input-block">
            <input id="JoinTime" type="text" class="layui-input" name="JoinTime" lay-verify="datetime" autocomplete="off" readonly="readonly" value="@(Model.JoinTime==null?DateTime.Now.ToString("yyyy-MM-dd"):Convert.ToDateTime(Model.JoinTime).ToString("yyyy-MM-dd"))">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="Status" lay-verify="required" lay-search="">
                <option value="">请选择</option>
                <option value="1" @(Model.Status == 1 || Model.Status == 0 ? "selected=selected" : "")>启用</option>
                <option value="2" @(Model.Status == 2 ? "selected=selected" : "")>禁用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" align="center">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="Save">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
    </div>
</form>

@section scripts{
    <script type="text/javascript">
        var $, layer;
        layui.use(['form', 'laydate'], function () {
            var form = layui.form;
            $ = layui.jquery;
            laydate = layui.laydate;
            layer = parent.layer === undefined ? layui.layer : parent.layer;
            laydate.render({ elem: '#JoinTime' });

            // 监听关闭
            $('#btnCancel').on('click', function () {
                layer_close();
            });

            /**地区三级联动*/
            form.on('select(Place)', function (data) {
                var text = $(data.elem).find("[value='" + data.value + "']").html();//获取选中的text
                $(data.elem).siblings("input:hidden").val(text);

                var urls = '@Url.Action("GetPlace", "Place")' + '?Pid=' + data.value;
                var name = $(data.elem).attr("name");
                if (name == "ProvinceCode") {
                    getJSON(urls, $("#CityCode"));
                    $("#DistrictCode").empty();//清空区数据
                }
                else if (name == "CityCode")
                    getJSON(urls, $("#DistrictCode"));
            });

            function getJSON(urls, even) {
                $.post(urls, function (json) {
                    var name = even.find("select").attr("name");
                    var select = '<select id="' + name + '" name="' + name + '" lay-filter="state" lay-verify="required">';
                    select += '<option value="">请选择</option>';
                    $(json).each(function () {
                        select += '<option value="' + this.Code + '">' + this.Name + '</option>';
                    });
                    select += '</select>';
                    even.html(select);
                    form.render('select');
                });
            }


            var postUrl = '@(Url.Action(Model.Id > 0 ? "Edit" : "Add", "Store"))';

            // 监听提交
            form.on('submit(Save)', function (data) {
                var loading = layer.load();
                $.post(postUrl, data.field,
                    function (result) {
                        layer.close(loading);
                        if (result.Success) {
                            layer.msg(result.Message, { icon: 1, time: 2000 });
                            parent.reload();
                            layer_close();
                        } else {
                            layer.alert(result.Message, { icon: 2 });
                        }
                    });
                return false;
            })

        });
    </script>
}



